#import "NoteNode.h"

@implementation NoteNode

@synthesize note, pos;
@synthesize isNote;
@synthesize isRest;
@synthesize parent;
@synthesize children;
@synthesize noteView;
@synthesize button;
@synthesize rest;
@synthesize tag;

/*
 Initiates a NodeNode with default boolean settings, nil parent and whole note
 */
-(id)initWholeNode {
	if (self = [super init]) {
		note = [[Note alloc] initWholeNote];
		isNote = false;
		isRest = true;
		parent = nil;
		children = [[NSMutableArray alloc] initWithCapacity: 0];
		button = nil;
		rest = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wholeRest.png"]];
		rest.frame = CGRectMake(175, 66, 50, 15);
	}
	return self;
}

/*
 Initiates a NodeNode with default boolean settings, given parent and half note
 */
-(id)initHalfNode:(NoteNode*)parentNode {
	if (self = [super init]) {
		note = [[Note alloc] initHalfNote];
		isNote = FALSE;
		isRest = TRUE;
		parent = parentNode;
		children = [[NSMutableArray alloc] initWithCapacity: 0];
		button = nil;
		rest = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"halfRest.png"]];
		rest.frame = CGRectMake(75, 83, 50, 15);
	}
	return self;
}

/*
 Initiates a NodeNode with default boolean settings, given parent and quarter note
 */
-(id)initQuarterNode:(NoteNode*)parentNode {
	if (self = [super init]) {
		note = [[Note alloc] initQuarterNote];
		isNote = FALSE;
		isRest = TRUE;
		parent = parentNode;
		children = [[NSMutableArray alloc] initWithCapacity: 0];
		button = nil;
		rest = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"quarterRest.png"]];
		rest.frame = CGRectMake(35, 50, 30, 90);
	}
	return self;
}

/*
 Initiates a NodeNode with default boolean settings, given parent and eighth note
 */
-(id)initEighthNode:(NoteNode*)parentNode {
	if (self = [super init]) {
		note = [[Note alloc] initEighthNote];
		isNote = FALSE;
		isRest = TRUE;
		parent = parentNode;
		children = [[NSMutableArray alloc] initWithCapacity: 0];
		button = nil;
		rest = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"eighthRest.png"]];
		rest.frame = CGRectMake(7.5, 67, 35, 60);
	}
	return self;
}

/*
 Draws all graphic elements associated with node
 */
-(void)drawNode {
	if (isRest) {
		[noteView addSubview:rest];
		//[noteView setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5]];
	}
	else if (isNote) {
		[rest removeFromSuperview];
		//[noteView setBackgroundColor:[UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:0.5]];
	}
	[button setHidden:isRest];
	[rest setHidden:isNote];
}

/*
 Adds given childNode into children array
 */
-(void)addChild:(NoteNode*)childNode {
	[children addObject: childNode];
	childNode.parent = self;
	return;
}

/*
 Recursively sets all descendent NoteNodes with default boolean settings
 */
-(void)setDescendants {
	if ([children count] == 0){
		isNote = FALSE;
		isRest = TRUE;
		return;
	}
	for (NoteNode* child in children){
		[child setDescendants];
	}
	isNote = FALSE;
	isRest = TRUE;
	return;
}

/*
 Recursively sets all parental NoteNodes to correct false false boolean setting
 */
-(void)setAncestors {
	if (parent != nil){
		[parent setAncestors];
	}
	isNote = FALSE;
	isRest = FALSE;
	return;
}

/*
 Resets ancestors
 */
-(void)resetAncestors {
	if (parent != nil){
		[parent resetAncestors];
	}
	if (![self hasNoteChild]){
		isNote = FALSE;
		isRest = TRUE;
	}
	return;
}

/*
 Returns if any descendants have note value true
 */
-(BOOL)hasNoteChild {
	BOOL hasNote = FALSE;
	if ([children count] == 0){
		return isNote;
	}
	for (NoteNode* child in children){
		if ([child hasNoteChild]){
			hasNote = TRUE;
		}
	}
	if (isNote){
		hasNote = TRUE;
	}
	return hasNote;
}

-(void)clearAllChildren {
	if (children.count == 0){
		[rest setHidden:TRUE];
		return;
	}
	for (NoteNode *child in children){
		[child clearAllChildren];
	}
	[rest setHidden:TRUE];
}


-(id)initWithCoder:(NSCoder*)coder {
	note = [[coder decodeObjectForKey:@"note"] retain];
	isNote = [coder decodeBoolForKey:@"isNote"];
	isRest = [coder decodeBoolForKey:@"isRest"];
	children = [[NSMutableArray alloc] initWithCapacity: 0];
	NSData *pngData = [coder decodeObjectForKey:@"PNGRepresentation"];
	UIImage *image = [[UIImage alloc] initWithData:pngData];
	[rest initWithImage: image];
	pos.x = [coder decodeIntForKey:@"x"];
	pos.y = [coder decodeIntForKey:@"y"];
	tag = [coder decodeIntForKey:@"tag"];
	return self;
}

-(void)encodeWithCoder:(NSCoder*)encoder {
	[encoder encodeObject: note forKey: @"note"];
	[encoder encodeBool: isNote forKey: @"isNote"];
	[encoder encodeBool: isRest forKey: @"isRest"];
	[encoder encodeInt: button.center.x forKey: @"x"];
	[encoder encodeInt: button.center.y forKey: @"y"];
	[encoder encodeInt: button.tag forKey: @"tag"];
	[encoder encodeObject:UIImagePNGRepresentation(rest.image) forKey:@"PNGRepresentation"];
}

@end
